package sch.frog.behavioral.observer;

import java.util.HashSet;
import java.util.Set;

public class Subject {

    private Set<Observer> observers = new HashSet<>();

    public void attach(Observer o){
        observers.add(o);
    }

    public void detach(Observer o){
        observers.remove(o);
    }

    public void doSomething(){
        int i = 0;
        while(i < 10000){
            // do something...
            i++;
        }

        // after a while ...
        this.notifyObserver();  //工作执行完成, 通知所有观察者
    }

    private void notifyObserver(){
        for(Observer o : observers){
            o.update();
        }
    }

}
